package com.darco.darcoapp;

import java.util.ArrayList;
import java.util.Vector;
import java.util.zip.Inflater;

import com.darco.darcoapp.db.DarcoData;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.util.Pair;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import static DARCOShared.utils.Constants.*;

public class List_Activity extends ListActivity {
	
	private ArrayList<ContentValues> cardList;
	private ArrayList<ContentValues> uniqueList;
	private ContentValues account;
	private ContentValues system;
	private Bundle extra;
	private DarcoData d;
	private int currentPosition;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.list_single_element);
		d= new DarcoData(this);
		extra= getIntent().getExtras();
		if(extra!=null){
			try{
				account= (ContentValues) extra.get("account");
				system= (ContentValues) extra.get("system");
				cardList = (ArrayList<ContentValues>) extra.get("cardList");
				uniqueList = (ArrayList<ContentValues>) extra.get("uniqueList");
				
				
				if(cardList.size()!=uniqueList.size()){
					Log.d("vector sizes", "card list size != unique list size");
					return;
				}
			
				String cardName[]= new String[cardList.size()];
				String cardCat[]= new String[cardList.size()];
				String cardId[]= new String[cardList.size()];
				Bitmap cardPreview[]= new Bitmap[cardList.size()];
				
				//inserimento dati negli array
				for(int i=0;i<cardList.size();i++){
					cardName[i] = cardList.get(i).getAsString(CARD_NAME);
					cardCat[i]= cardList.get(i).getAsString(CARD_CATEGORY_NAME) + " - " +
							cardList.get(i).getAsString(CARD_SUBCATEGORY_NAME);
					cardId[i]=uniqueList.get(i).getAsString(UNIQUECARD_ID);
					byte[] prev = cardList.get(i).getAsByteArray(CARD_PREVIEW);
					cardPreview[i] = BitmapFactory.decodeByteArray(prev,0,prev.length);;		
				}
				
				AdapterListCard adp = new AdapterListCard(this, R.layout.list_single_element, cardName, cardCat, cardId, cardPreview);
				setListAdapter(adp);
				
				//inserimento dati nel DB con thread
				d.insertCardListIntoLocalDBAsync(uniqueList, cardList);
				
			
			
			}catch(ClassCastException e){e.printStackTrace();}
		
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_list_, menu);
		return true;
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		ContentValues u= uniqueList.get(position);
		ContentValues c= cardList.get(position);
		currentPosition = position;
		
		Intent i= new Intent(this, Visualization_Activity.class);
		i.putExtra("account", account);
		i.putExtra("system", system);
		i.putExtra("cardInfo", c);
		i.putExtra("uniqueInfo", u);
		i.putExtra(VISUALIZATION_ORIGIN, VISUALIZATION_FROM_CARD_LIST);
		startActivityForResult(i, VISUALIZATION_FROM_CARD_LIST);
	  }
	
	protected void onActivityResult (int requestCode, int resultCode, Intent data){
		if(resultCode==Visualization_Activity.CARD_SENT){
			finish();
		}
		
	}
	
	
	

}
